/**
 * 
 */
package com.zx.cloud.utils;


import java.net.InetAddress;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.servlet.http.HttpServletRequest;
/**
 * @author 作者姓名 E-mail: email地址
 * @version 创建时间：2017年10月10日 上午9:44:35
 * 类说明
 */
/**
 * @author Administrator
 *
 */
public class Sequence {
	 private static final long ONE_STEP = 10;
	    private static final Lock LOCK = new ReentrantLock();
	    private static long lastTime = System.currentTimeMillis();
	    private static short lastCount = 0;
	    private static int count = 0;

	    @SuppressWarnings("finally")
	    public static String nextId() 
	    {
	        LOCK.lock();
	        try {
	            if (lastCount == ONE_STEP) {
	                boolean done = false;
	                while (!done) {
	                    long now = System.currentTimeMillis();
	                    if (now == lastTime) {
	                        try {
	                            Thread.currentThread();
	                            Thread.sleep(1);
	                        } catch (java.lang.InterruptedException e) {
	                        }
	                        continue;
	                    } else {
	                        lastTime = now;
	                        lastCount = 0;
	                        done = true;
	                    }
	                }
	            }
	            count = lastCount++;
	        }
	        finally 
	        {
	            LOCK.unlock();
	            return lastTime+""+String.format("%03d",count); 
	        }
	    }
	    
	    
	    public static String getClientIpAddr() throws Exception {
	        InetAddress ia=null;
	        String localip=null;
	        try {
	            ia=ia.getLocalHost();
	            
	            String localname=ia.getHostName();
	             localip=ia.getHostAddress();
	           /* System.out.println("本机名称是："+ localname);
	            System.out.println("本机的ip是 ："+localip);*/
	        } catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        
	        return localip;
      }

	    public static void main(String[] args)
	    {
	        //测试
	        for(int i=0;i<1000;i++)
	        {
	            System.out.println(nextId());
	        }        
	    }
}
